<template>
  <el-row id="login" type="flex">
    <el-col :span="8">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>用户登录</span>
        </div>
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginForm"
          label-width="100px"
          class="demo-loginForm"
        >
          <el-form-item label="用户名" prop="userName">
            <el-input
              v-model="loginForm.userName"
              @keyup.enter.native="submitForm('loginForm')"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              show-password
              v-model="loginForm.password"
              @keyup.enter.native="submitForm('loginForm')"
            ></el-input>
          </el-form-item>
          <el-form-item id="btn">
            <el-button type="primary" @click="submitForm('loginForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <p class="text item">
          *忘记密码请联系管理员重置<br />*又是愉快的一天<br />*努力工作！！！
        </p>
      </el-card></el-col
    >
    <el-col :span="8"> </el-col>
    <el-col :span="8"></el-col>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        userName: "1347990476",
        password: "123456",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9_-]{5,11}$/,
            message: "格式错误",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          this.$axios
            .post("/login", this.$qs.stringify(this.loginForm), {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
            })
            .then((res) => {
              if (res.data.success) {
                const jwt = res.headers["authorization"];
                this.$store.commit("SET_TOKEN", jwt);
                this.$router.push("/index");
                this.$message({
                  type: "success",
                  message: "登录成功!",
                  showClose: true,
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
.box-card {
  width: 440px;
  height: 400px;
  margin-top: 80px;
  margin-left: 70px;
  background-image: url("../assets/images/login_card.png");
  background-size: 100% 100%;
}
.el-row {
  height: 100%;
  display: flex;
}
#login {
  background-image: url("../assets/images/login.png");
  background-size: 100% 100%;
}
#btn {
  margin-top: 10%;
}
.text {
  font-family: "楷体";
  font-size: 14px;
  color: rgb(255, 70, 70);
}

.item {
  padding: 55px 0px 0px 30px;
}
</style>